package com.upc.toss.util;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.iotda.v5.IoTDAClient;
import com.huaweicloud.sdk.iotda.v5.model.*;
import com.huaweicloud.sdk.iotda.v5.region.IoTDARegion;
import ohos.agp.components.webengine.LocationAccessController;
import ohos.ai.cv.sr.SrParamKey;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Update {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
    public static void postCommand(String commandName,String paraName,String paraValue){

        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(Access.AK)
                .withSk(Access.SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();

        // 实例化请求对象

        CreateCommandRequest request =new CreateCommandRequest();
        DeviceCommandRequest dRequest=new DeviceCommandRequest();

        Map<String,String> parasMap=new HashMap<String,String>();
        parasMap.put(paraName,paraValue);
        dRequest.setCommandName(commandName);
        dRequest.setParas(parasMap);
        dRequest.setServiceId(Access.SERVICE_ID);

        request.setBody(dRequest);
        request.setDeviceId(Access.DEVICE_ID);


        try {
            client.createCommand(request);

        }catch (ConnectionException | RequestTimeoutException e) {
            HiLog.error(LABEL,e.getMessage());
        } catch (ServiceResponseException e) {
            HiLog.error(LABEL,e.getErrorMsg());
        }

    }
    public static void postCommandValue(String commandName,String type,String thl){
        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(Access.AK)
                .withSk(Access.SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();

        //实例化请求对象
        CreateCommandRequest request =new CreateCommandRequest();
        DeviceCommandRequest dRequest=new DeviceCommandRequest();

        Map<String, String> parasMap=new HashMap<String, String>();
        parasMap.put(type,thl);
        dRequest.setCommandName(commandName);
        dRequest.setParas(parasMap);
        dRequest.setServiceId(Access.SERVICE_ID);

        request.setBody(dRequest);
        request.setDeviceId(Access.DEVICE_ID);
        try {
            client.createCommand(request);

        }catch (ConnectionException | RequestTimeoutException e) {
            HiLog.error(LABEL,e.getMessage());
        } catch (ServiceResponseException e) {
            HiLog.error(LABEL,e.getErrorMsg());
        }
    }
    public static void updateSetting(String tep,String hum,String lum){

        // 创建认证
        ICredential auth = new BasicCredentials()
                .withAk(Access.AK)
                .withSk(Access.SK);
        // 创建IoTDAClient实例并初始化
        IoTDAClient client = IoTDAClient.newBuilder()
                .withCredential(auth)
                .withRegion(IoTDARegion.CN_NORTH_4)
                .build();

        UpdateDesireds updateDesireds = new UpdateDesireds();
        UpdateDesired updateDesiredTep = new UpdateDesired();
        UpdateDesired updateDesiredHum = new UpdateDesired();
        UpdateDesired updateDesiredLum = new UpdateDesired();


        String tepStr ="{" + "temperature"+":"+tep+"}";
        String humStr ="{" + "Humanity"+":"+hum+"}";
        String lumStr ="{" + "Luminance"+":"+lum+"}";

        updateDesiredTep.setDesired(tepStr);
        updateDesiredHum.setDesired(humStr);
        updateDesiredLum.setDesired(lumStr);
        updateDesiredTep.setServiceId(Access.SERVICE_ID);
        updateDesiredHum.setServiceId(Access.SERVICE_ID);
        updateDesiredLum.setServiceId(Access.SERVICE_ID);

        List<UpdateDesired> shadowDesire = new ArrayList<>();
        shadowDesire.add(updateDesiredTep);
        shadowDesire.add(updateDesiredHum);
        shadowDesire.add(updateDesiredLum);


        updateDesireds.setShadow(shadowDesire);

        // 实例化请求对象
        UpdateDeviceShadowDesiredDataRequest request=new UpdateDeviceShadowDesiredDataRequest();
        request.setDeviceId(Access.DEVICE_ID);
        request.setBody(updateDesireds);
        try {
            UpdateDeviceShadowDesiredDataResponse response=client.updateDeviceShadowDesiredData(request);


        }catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
            HiLog.error(LABEL,e.getMessage());
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            HiLog.error(LABEL,e.getErrorMsg());
        }

    }



}
